﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.IO;
using System.Web;

namespace POSSystem.BLL
{
    public class ProductImages : _ProductImages
    {
        public static ProductImages GetById(int id)
        {
            ProductImages prd = new ProductImages();
            prd.LoadByPrimaryKey(id);
            return prd;
        }
        public static void DeleteById(int id)
        {
            ProductImages imgs = new ProductImages();
            imgs.LoadByPrimaryKey(id);
            if(imgs.RowCount > 0)
            {
                int productID = imgs.ProductID;
                bool def = imgs.Default;
                string thumb = imgs.ThumbUrl;
                string org = imgs.OriginalUrl;

                imgs.Deleted = true;
                imgs.Save();

                //try
                //{
                //    File.Delete(HttpContext.Current.Server.MapPath(thumb));
                //}
                //catch (Exception ex) { }

                //try
                //{
                //    File.Delete(HttpContext.Current.Server.MapPath(org));
                //}
                //catch (Exception ex) { }

                if (def)
                {
                    ProductImages imgs1 = new ProductImages();
                    imgs1.Where.ProductID.Value = productID;
                    imgs1.Query.Top = 1;
                    imgs1.Query.Load();
                    imgs.Default = true;
                    imgs.Save();
                }
            }
        }

        public static ProductImages GetDefaultPicture(int productId)
        {
            ProductImages pi = new ProductImages();
            pi.Where.ProductID.Value = productId;
            pi.Where.Default.Value = true;
            pi.Where.Deleted.Value = false;
            pi.Query.Load();

            return pi;
        }

        public static ProductImages GetProductPictures(int productId)
        {
            ProductImages pi = new ProductImages();
            pi.Where.ProductID.Value = productId;
            pi.Where.Deleted.Value = false;
            pi.Query.Load();

            return pi;
        }

        public static void SetDefaultPicture(int id)
        {
            ProductImages pi = new ProductImages();
            pi.LoadByPrimaryKey(id);
            if (pi.RowCount > 0)
            {
                ProductImages defaultPi = ProductImages.GetDefaultPicture(pi.ProductID);
                if (defaultPi.RowCount > 0)
                {
                    defaultPi.Default = false;
                    defaultPi.Save();
                    
                }
                pi.Default = true;
                pi.Save();
            }
        }

        public static void AddNewImage(bool def, int productId, string description, string orginalUrl, string thumbUrl)
        {
            ProductImages imgs = new ProductImages();
            imgs.AddNew();
            imgs.Default = def;
            imgs.ProductID = productId;
            imgs.Description = description;
            imgs.OriginalUrl = orginalUrl;
            imgs.ThumbUrl = thumbUrl;
            imgs.Deleted = false;
            imgs.Save();
        }


        public static void Rename(int id, string newName)
        {
            ProductImages pi = new ProductImages();
            pi.LoadByPrimaryKey(id);
            if (pi.RowCount > 0)
            {
                pi.Description = newName;
                pi.Save();
            }
        }
    }
}
